#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
将每一行是 \t分隔的文件转为csv
'''
import sys
import csv

class TrunToCsv:
    '''
    目前只支持两种文件转换
    1、文件每一行是由 \t分割的
    2、文件每一行是一个list字符串

    '''
    def __init__(self,abs_file_path,file_type):
        self.abs_file_path = abs_file_path

    def file_to_csv(self,abs_file_path):
        with open(abs_file_path, 'r') as f, open(abs_file_path + '.csv', 'w') as csvfile:
            writer = csv.writer(csvfile, delimiter=',', dialect='excel')
            for line in f:
                self.handle_one_tab_line(writer,line)

    def handle_one_tab_line(self,writer,line):
        '''
        一行行写入csv文件
        @param writer:csv写对象
        @param line: 一行原始文件数据
        @return:
        '''
        line = line.strip()
        row_values = line.split('\t')
        writer.writerow(row_values)


def file_to_csv(abs_file_path):
        with open(abs_file_path,'r') as f,open(abs_file_path+'.csv', 'w') as csvfile:
            writer = csv.writer(csvfile, delimiter=',', dialect='excel')
            for line in f:
                line = line.strip()
                row_values = line.split('\t')
                writer.writerow(row_values)

inp = sys.argv
print(inp)
file_name = inp[1]
file_to_csv(file_name)
print('转为csv success at{}'.format(file_name))
